<template>
  <div class="table-page">
    <GiTable
        row-key="id"
        title="双选会企业岗位管理"
        :data="dataList"
        :columns="columns"
        :loading="loading"
        :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
        :pagination="pagination"
        :disabled-tools="['size']"
        :disabled-column-keys="['name']"
        @refresh="search"
    >
      <template #custom-left>
        <a-input v-model="queryForm.title" placeholder="请输入岗位名称" allow-clear @change="search">
          <template #prefix>
            <icon-search/>
          </template>
        </a-input>
        <a-input v-model="queryForm.salary" placeholder="请输入薪水" allow-clear @change="search">
          <template #prefix>
            <icon-search/>
          </template>
        </a-input>
        <a-input v-model="queryForm.location" placeholder="请输入工作地点" allow-clear @change="search">
          <template #prefix>
            <icon-search/>
          </template>
        </a-input>
        <a-button @click="reset">重置</a-button>
      </template>
      <template #custom-right>
        <a-button v-permission="['jobfair:jobPosition:add']" type="primary" @click="onAdd">
          <template #icon>
            <icon-plus/>
          </template>
          <span>新增</span>
        </a-button>
      </template>
      <template #name="{ record }">
        <a-link @click="onDetail(record)">{{ record.name }}</a-link>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link @click="searchRecord(record)">查看投递情况</a-link>
          <a-link @click="onUpdate(record)">修改</a-link>
          <a-link
              status="danger"
              :disabled="record.disabled"
              @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <JobPositionAddModal ref="JobPositionAddModalRef" @save-success="search" :companyId="companyId as any"
                         :jobfairId="jobfairId as any"/>
    <JobPositionDetailDrawer ref="JobPositionDetailDrawerRef"/>
  </div>
</template>

<script setup lang="ts">
import JobPositionAddModal from './JobPositionAddModal.vue'
import JobPositionDetailDrawer from './JobPositionDetailDrawer.vue'
import {
  type JobPositionResp,
  type JobPositionQuery,
  deleteJobPosition,
  exportJobPosition,
  listJobPosition
} from '@/apis'
import type {TableInstanceColumns} from '@/components/GiTable/type'
import {useDownload, useTable} from '@/hooks'
import {isMobile} from '@/utils'
import has from '@/utils/has'
import {useDict} from '@/hooks/app'
import {Message} from "@arco-design/web-vue";
import router from "@/router";

defineOptions({name: 'JobPositionManage'})
const route = useRoute();
const companyId = route.query.companyId; // 获取传递的 id
const jobfairId = route.query.jobfairId;

const queryForm = reactive<JobPositionQuery>({
  companyId: "",
  jobFairId: "",
  title: "",
  salary: "",
  location: "",
  sort: ['createTime,desc']
})


const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listJobPosition({...queryForm, ...page}), {immediate: true})

const columns: TableInstanceColumns[] = [
  {title: '名称', dataIndex: 'title', slotName: 'title'},
  {title: '薪水', dataIndex: 'salary', slotName: 'salary'},
  {title: '工作地点', dataIndex: 'location', slotName: 'location'},
  {
    title: '操作',
    slotName: 'action',
    width: 300,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
  }
]

// 重置
const reset = () => {
  queryForm.companyId = ""
  queryForm.jobFairId = ""
  queryForm.title = ""
  queryForm.salary = ""
  queryForm.location = ""
  search()
}

// 删除
const onDelete = (item: JobPositionResp) => {
  return handleDelete(() => deleteJobPosition(item.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

const JobPositionAddModalRef = ref<InstanceType<typeof JobPositionAddModal>>()
// 新增
const onAdd = () => {
  JobPositionAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (item: JobPositionResp) => {
  JobPositionAddModalRef.value?.onUpdate(item.id)
}

const JobPositionDetailDrawerRef = ref<InstanceType<typeof JobPositionDetailDrawer>>()
// 详情
const onDetail = (item: JobPositionResp) => {
  JobPositionDetailDrawerRef.value?.onDetail(item.id)
}

// 查看岗位投递情况
const searchRecord = (record: any) => {
  router.push({
    path: '/jobpositionresume',
    query: {positionId: record.id},
  })
}
</script>

<style lang="scss" scoped></style>
